/*********** NOTES HEADER ***********

DESCRIPTION: Means of health outcomes among treated and control patients
 separately by leads and lags, as determined by match

Datasets used: 
 (1) MatchedSample_Full

Datasets created: 
 (1) all_means 
 (2) paralleltrends_summary_sm
 
 *********** ************************/
set more off
set matsize 10000
capture log close
clear all
 
cd "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Program\PropensityScore\Patients\ResponsetoReferee\LeadsLags"

global origData "N:\MedicareClaims-P045601-BE"
global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients"
global logs "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "N:\MedicareClaims-P045601-BE\Work\ska"

adopath +  "N:/SIL-Common/estout"
adopath +  "N:/SIL-Common/outreg2"
adopath +  "N:/SIL-Common/reghdfe-master/package"

log using "./1b.ParallelTrends_SimpleMeans.log", replace

   
	use ../OtherPropScoreMethods/BiasAdjustment/MatchedSample_Full.dta
		capture drop leadlag 
		*Lead and lag variables
		 * Need this for the control group timing
		 egen qom=min(q_of_merger) , by(propScoreId)
		drop if qom==.  
		gen leadlag=qtr-qom
		replace leadlag=20 if leadlag>20
		replace leadlag=-16 if leadlag<-16
		replace leadlag= leadlag+16

		*Health Outcomes
		*glaucoma
		gen ocglaucoma = oc_glaucoma if kv_glaucoma==1 
		 replace ocglaucoma =. if kv_glaucoma==0 
		*diabetes 1
		gen ocdiabcomp1 = oc_diabcomp1 if kv_diabcomp1==1 
		 replace ocdiabcomp1=. if kv_diabcomp1==0 
		*diabetes 2
		gen ocdiabcomp2 = oc_diabcomp2 if kv_diabcomp2==1 
		 replace ocdiabcomp2=. if kv_diabcomp2==0 

		*ischemic heart disease2
		gen ocihd = oc_ihd if kv_ihd==1 
		 replace ocihd  =. if kv_ihd==0 
		*ami heart attack
		gen ocami= oc_ami if kv_ami==1 
		 replace ocami=. if kv_ami==0 
		*acute cardiac condition
		gen ocacuteicd8 = oc_acuteicd8 if kv_acuteicd8==1 
		 replace ocacuteicd8=. if kv_acuteicd8==0 


	*Means in sample by sex acquisition and lead lag; diabetes	 
	preserve
		collapse (mean) oc_death ocglaucoma ocdiabcomp1 ocdiabcomp2  if flag3==1 , by(leadlag SEX* sex_pat vmerger)   
		sort leadlag sex_pat vmerger 
		save ./diabetes_means, replace
	restore

	*Means in sample by sex acquisition and lead lag; hypertension	 
	preserve
		collapse (mean) oc_death ocihd ocami ocacuteicd8 if flag2==1 , by(leadlag SEX* sex_pat vmerger)   
		sort leadlag sex_pat vmerger 
		save ./hypertension_means, replace
	restore
	 
	*Means in sample by sex acquisition and lead lag; overall (death)	 
	preserve
		collapse (mean) oc_death if flag1==1 , by(leadlag SEX* sex_pat vmerger)   
		sort leadlag sex_pat vmerger 
		save ./all_means, replace
	restore

	clear

	*PUT it together
	use ./all_means
	merge 1:1 leadlag sex_pat vmerger using ./hypertension_means
		drop _merge

	merge 1:1 leadlag sex_pat vmerger using ./diabetes_means

		rename  (ocihd ocami ocacuteicd8  ocglaucoma ocdiabcomp1 ocdiabcomp2) (oc_ihd  oc_ami oc_acuteicd8 oc_glaucoma oc_diabcomp1 oc_diabcomp2 )

	save ./paralleltrends_summary_sm


 

